package adminorg;


import com.oros.db.*;
import com.vaadin.data.Container.Filter;
import com.vaadin.data.util.filter.Compare.Equal;
//import com.vaadin.data.util.filter.Compare.Equal;
import java.io.Serializable;

import java.util.LinkedHashMap;
public class DocumentView implements Serializable {

	private static final long serialVersionUID = -3727342245511010233L;
	private AEViewContainer DocumentViewContainer = null;
	  private AEViewContainer DocRightViewContainer=null,workDocumentViewContainer = null;
	  private AEViewContainer DocPostViewContainer = null;
	  private AEViewContainer OrgschemaViewContainer = null;
	  private AEViewContainer DictStatisticViewContainer = null;
	  private AEViewContainer StatisticUseViewContainer = null;
	  private AEViewHierarchicalContainer OrgschemaHierarchicalContainer = null;
	  private AEViewHierarchicalContainer OrgschemaHierContainer = null;
	  private AEViewContainer StatisticContainer = null;
	  private AEViewContainer EventPostTaskContainer = null;
      private DatabaseHelper dbHelp;
      private LinkedHashMap<String,String> documentTypesMap=null;
      private LinkedHashMap<Integer,String> rightNameMap = null;
      final public static int DOCUMENT_RIGHT_CREATOR = 1;
      final public static int DOCUMENT_RIGHT_WRITER = 2;
      final public static int DOCUMENT_RIGHT_READER = 3;
      final public static Integer DOC_RIGHT_CREATOR = new Integer(DOCUMENT_RIGHT_CREATOR);
      final public static Integer DOC_RIGHT_WRITER = new Integer(DOCUMENT_RIGHT_WRITER);
      final public static Integer DOC_RIGHT_READER = new Integer(DOCUMENT_RIGHT_READER);
      public static int columnDocumentsView_Link,columnDocumentsView_Type,
          columnDocumentsView_Right,columnDocumentsView_Name;
      public static int columnDocrightsView_Link;
      public DocumentServe docservice;
      public DocumentView(DatabaseHelper dbHelp)
      {
    	  this.dbHelp = dbHelp;
    	  docservice=new DocumentServe(dbHelp);
      }
	   public LinkedHashMap<String,String> getDocumentTypesMap()
	   {
		   if (documentTypesMap==null)
		   {
			   documentTypesMap=new LinkedHashMap<String,String>();
			   documentTypesMap.put("document", dbHelp.getWords("TextDocument"));
			   documentTypesMap.put("spreadsheet", dbHelp.getWords("Spreadsheet"));
		   }
		   return documentTypesMap;
	   }
	   public LinkedHashMap<Integer,String> getRightNameMap()
	   {
		   if (rightNameMap==null)
		   {
			   rightNameMap=new LinkedHashMap<Integer,String>();
			   rightNameMap.put(DOC_RIGHT_CREATOR, dbHelp.getWords("Creator"));
			   rightNameMap.put(DOC_RIGHT_WRITER, dbHelp.getWords("Writer"));
			   rightNameMap.put(DOC_RIGHT_READER, dbHelp.getWords("Reader"));
		   }
		   return rightNameMap;
	   }
	   public void checkDocumentRights(Object docId) throws Exception
	   {
		   docservice.notifyOfDocumentPostRightChange(docId);
	   }
	   public AEViewContainer getDocumentViewContainer() throws Exception
	    {
		 //by Sybase ASE SQL & Microsoft SQL standards:  
		 // create view DocView as 
		 //select * from docrights join documents on documents.id=docrights.docid
		// join orgschema on orgschema.id = docrights.post
		// join compacc on compacc.id = orgschema.accid
		// where compacc.email="%email%" order by docrights.right,documents.name_doc   
	    	if (DocumentViewContainer==null)
	    	{
	    		DocumentViewContainer = new AEViewContainer(
	    				"DocView",  //view name
	    				new AETableContainer[]{  // from docrights,documents,orgschema,compacc 
	    						dbHelp.getDocRightContainer(),
	    						dbHelp.getDocumentContainer(),  // 
	    						dbHelp.getOrgschemaReadContainer(),
	    						dbHelp.getCompAccContainer()},
	    				new boolean[] { //false = insertable & updatable & deletable container
	    						        // true = read-only container
	    						false, //we can insert & delete & update docrights from this view
	    						false, //we can insert & delete & update documents from this view
	    						true, //orgschema is a read-only container for this view
	    						true  //compacc is a read-only container for this view
	    				},		
	    				new String[] {  //columns of joining (the pair column is always a primary key)
	    				  "docrights.docid",   //inner join docrights on documents.id = docrights.id
	    				  "docrights.post",  //inner join orgschema on documents.post=orgschema.id
	    				  "orgschema.accid"  //inner join compacc on orgschema.accid = compacc.id
	    				},
	    				new int[] { // left outer join = 1, inner join = 0
	    	   					0,   //inner join docrights
	    	   				    0,  // inner join orgschema
	    	   				    0   //inner join compacc
	    				},
	    				new Filter[] { //where clause (not including join conditions)
	    						// where compacc.email="...email..."
	    						new EqualToUserFilter("compacc.email",dbHelp)	
	    				},
	    				new String[] { //sorting: null or empty array assume no sorting
	    					"docrights.right",
	    					"documents.name_doc"
	    				},
	    				null, //sorting order boolean[] ascending: null assumes always ascending 
                        null, //LinkedHashMap<String,String> ColumnNameAliases: null assumes no aliases	
                        new AEGeneratedColumn[] { //generated columns: (null if not used)
	    						//we need 3 columns: post_name, doctype_name,right_title
	    						// 1. post name = orgschema.name + orgschema.dept_name
	    						new AEGeneratedColumn("doctype_name",String.class,"",
	    								new AEGeneratedListData("documents.file_type",getDocumentTypesMap())),
	    						new AEGeneratedColumn("right_title",String.class,"",
	    								new AEGeneratedListData("docrights.right",getRightNameMap()))		
	    				},
	    				null  //groupBy is not implemented yet (as of March 28, 2012)and must be null 
	    				);
	    		columnDocumentsView_Link=DocumentViewContainer.getColumnIndex("documents.link");
	    		columnDocumentsView_Type=DocumentViewContainer.getColumnIndex("documents.file_type");
	    		columnDocumentsView_Right=DocumentViewContainer.getColumnIndex("docrights.right");
	    		columnDocumentsView_Name=DocumentViewContainer.getColumnIndex("documents.name_doc");
	    	}
	    	return DocumentViewContainer;
	    }
			   
	   public AEViewContainer getDocRightViewContainer() throws Exception
	    {
		   //create view DocRightView as 
		   // select *,post_name from docrights join orgschema 
		   //   on orgschema.id=docrights.post
		   // order by docrights.right,post_name
		   //Note: post_name is a generated field
	    	if (DocRightViewContainer==null)
	    	{
	    		DocRightViewContainer = new AEViewContainer(
	    				"DocRightView", //view name: create view DocRightView as ...
	    				new AETableContainer[]{  // from docrights,orgschema 
	    						dbHelp.getDocRightContainer(),
	    						dbHelp.getOrgschemaReadContainer()
	    				},
	    				new boolean[] { //false = insertable & updatable & deletable container
						        // true = read-only container
						        false, //we can insert & delete & update docrights from this view
						        true   //orgschema is a read-only container for this view
	    				},
	    				new String[] {  //columns of joining (the pair column is always a primary key)
	    				  "docrights.post"  //inner join orgschema on documents.post=orgschema.id
	    				},
	    				new int[] { // left outer join = true, inner join = false
	    	   				    0  // inner join orgschema
	    				},
	    				null,   //Filter[]: where clause (not including join conditions)
	    				       //in this view filters will be added later
	    				new String[] { //sorting: null or empty array assume no sorting
	    					"docrights.right",
	    					"post_name" //this is a generated column
	    				},
	    				null, //sorting order boolean[] ascending: null assumes always ascending 
                       null, //LinkedHashMap<String,String> ColumnNameAliases: null assumes no aliases	
                       new AEGeneratedColumn[] { //generated columns: (null if not used)
	    						//we need 2 columns: post_name(already defined in orgschemareadcontainer), right_title
	    						// 1. post name = orgschema.name + orgschema.dept_name
	    						new AEGeneratedColumn("right_title",String.class,"",
	    								new AEGeneratedListData("docrights.right",getRightNameMap()))		
	    				},
	    				null  //groupBy is not implemented yet (as of March 28, 2012)and must be null 
	    				);
	    		columnDocrightsView_Link=DocumentViewContainer.getColumnIndex("documents.link");
	    	}
	    	return DocRightViewContainer;
	    }
	   public AEViewContainer getWorkDocumentViewContainer() throws Exception
	   {
		   if (workDocumentViewContainer==null)
		   {
			   workDocumentViewContainer =  new AEViewContainer(
	    				"ReadDocView",  //view name
	    				new AETableContainer[]{  // from docrights,documents,orgschema,compacc 
	    						dbHelp.getDocRightContainer(),
	    						dbHelp.getDocumentContainer(),  // 
	    						dbHelp.getOrgschemaReadContainer(),
	    						dbHelp.getCompAccContainer()},
	    				new boolean[] { //false = insertable & updatable & deletable container
	    						        // true = read-only container
	    						true, //we can insert & delete & update docrights from this view
	    						true, //we can insert & delete & update documents from this view
	    						true, //orgschema is a read-only container for this view
	    						true  //compacc is a read-only container for this view
	    				},		
	    				new String[] {  //columns of joining (the pair column is always a primary key)
	    				  "docrights.docid",   //inner join docrights on documents.id = docrights.id
	    				  "docrights.post",  //inner join orgschema on documents.post=orgschema.id
	    				  "orgschema.accid"  //inner join compacc on orgschema.accid = compacc.id
	    				},
	    				new int[] { // left outer join = 1, inner join = 0
	    	   					0,   //inner join docrights
	    	   				    0,  // inner join orgschema
	    	   				    0   //inner join compacc
	    				},
	    				null, //where clause (not including join conditions)
	    				null,  //sorting: null or empty array assume no sorting
	    				null, //sorting order boolean[] ascending: null assumes always ascending 
                        null, //LinkedHashMap<String,String> ColumnNameAliases: null assumes no aliases	
                        null, //generated columns: (null if not used)
	    				null  //groupBy is not implemented yet (as of March 28, 2012)and must be null 
	    				);
		   }
		   return workDocumentViewContainer;
	   }
	   public AEViewContainer getDocPostViewContainer() throws Exception
	    {
		   //create view DocRightView as 
		   // select *,post_name from docrights join orgschema 
		   //   on orgschema.id=docrights.post
		   // order by docrights.right,post_name
		   //Note: post_name is a generated field
	    	if (DocPostViewContainer==null)
	    	{
	    		DocPostViewContainer = new AEViewContainer(
	    				"DocPostView", //view name: create view DocRightView as ...
	    				new AETableContainer[]{  // from docrights,documents,orgschema,compacc 
	    						dbHelp.getOrgschemaReadContainer(),
	    						dbHelp.getCompAccContainer()
	    				},
	    				new boolean[] { //false = insertable & updatable & deletable container
						        // true = read-only container
						        true, //we can insert & delete & update docrights from this view
						        true   //orgschema is a read-only container for this view
	    				},
	    				new String[] {  //columns of joining (the pair column is always a primary key)
	    				  "orgschema.accid"  //inner join orgschema on documents.post=orgschema.id
	    				},
	    				new int[] { // left outer join = 1, inner join = 0
	    	   				    0  // inner join orgschema
	    				},
	    				new Filter[] {  //Filter[]: where clause (not including join conditions)
	    				       //in this view filters will be added later
	    						// where compacc.email="...email..."
	    						new EqualToUserFilter("compacc.email",dbHelp)	
	    				},
	    				new String[] { //sorting: null or empty array assume no sorting
	    					"orgschema.post_name" //this is a generated column
	    				},
	    				null, //sorting order boolean[] ascending: null assumes always ascending 
                        null, //LinkedHashMap<String,String> ColumnNameAliases: null assumes no aliases	
                        null,  //generated columns: (null if not used)
	    				null  //groupBy is not implemented yet (as of March 28, 2012)and must be null 
	    				);
	    	}
	    	return DocPostViewContainer;
	    }
	   public AEViewHierarchicalContainer getOrgschemaHierarchicalContainer() throws Exception
	    {
	    	if (OrgschemaHierarchicalContainer==null)
	    	{
	    		OrgschemaHierarchicalContainer = new AEViewHierarchicalContainer("pid",
	    				dbHelp.getOrgschemaReadContainer(),
	    				"OrgschemaHierarchicalView",  //view name
	    				new AETableContainer[]{  // from Orgschema,OrgLevel,compacc 
	    						dbHelp.getOrgschemaContainer(),
	    						dbHelp.getOrgLevelContainer(),  // 
	    						dbHelp.getCompAccContainer()},
	    				new boolean[] { //false = insertable & updatable & deletable container
	    						        // true = read-only container
	    						false, //we can insert & delete & update orgpost from this view
	    						true, //orglevel is a read-only container for this view
	    						true  //compacc is a read-only container for this view
	    				},		
	    				new String[] {  //columns of joining (the pair column is always a primary key)
	    				  "orgschema.level",   // join orglevel on orgschema.level = orglevel.id
	    				  "orgschema.accid"  // join compacc on orgschema.accid = compacc.id
	    				},
	    				new int[] { // left outer join = 1, inner join = 0
	    	   					1,   //left outer join orglevel
	    	   				    1  // left outer join compacc	    	   				    
	    				},
	    				null,
	    				new String[] { //sorting: null or empty array assume no sorting
	    					"orglevel.level",
	    					"orgschema.pid",
	    					"orgschema.id"
	    				},
	    				null, //sorting order boolean[] ascending: null assumes always ascending 
                        null, //LinkedHashMap<String,String> ColumnNameAliases: null assumes no aliases	
                        null,
	    				null  //groupBy is not implemented yet (as of March 28, 2012)and must be null 
	    				);
	    	}
	    	return OrgschemaHierarchicalContainer;
	    }
	   
	   public AEViewHierarchicalContainer getOrgschemaHierContainer() throws Exception
	    {
	    	if (OrgschemaHierContainer==null)
	    	{
	    		OrgschemaHierContainer = new AEViewHierarchicalContainer("pid",
	    				dbHelp.getOrgschemaReadContainer(),
	    				"OrgschemaHierView",  //view name
	    				new AETableContainer[]{  // from Orgschema,OrgLevel,compacc 
	    						dbHelp.getOrgschemaContainer(),
	    						dbHelp.getOrgLevelContainer(),  // 
	    						dbHelp.getCompAccContainer()},
	    				new boolean[] { //false = insertable & updatable & deletable container
	    						        // true = read-only container
	    						false, //we can insert & delete & update orgpost from this view
	    						true, //orglevel is a read-only container for this view
	    						true  //compacc is a read-only container for this view
	    				},		
	    				new String[] {  //columns of joining (the pair column is always a primary key)
	    				  "orgschema.level",   // join orglevel on orgschema.level = orglevel.id
	    				  "orgschema.accid"  // join compacc on orgschema.accid = compacc.id
	    				},
	    				new int[] { // left outer join = 1, inner join = 0
	    	   					1,   //left outer join orglevel
	    	   				    1  // left outer join compacc	    	   				    
	    				},
	    				null,
	    				new String[] { //sorting: null or empty array assume no sorting
	    					"orglevel.level",
	    					"orgschema.pid",
	    					"orgschema.id"
	    				},
	    				null, //sorting order boolean[] ascending: null assumes always ascending 
                       null, //LinkedHashMap<String,String> ColumnNameAliases: null assumes no aliases	
                       null,
	    				null  //groupBy is not implemented yet (as of March 28, 2012)and must be null 
	    				);
	    	}
	    	return OrgschemaHierContainer;
	    }
	   
	   public AEViewContainer getOrgschemaViewContainer() throws Exception
	    {
	    	if (OrgschemaViewContainer==null)
	    	{
	    		OrgschemaViewContainer = new AEViewContainer(
	    				"OrgschemaView",  //view name
	    				new AETableContainer[]{  // from Orgschema,OrgLevel,compacc 
	    						dbHelp.getOrgschemaContainer(),
	    						dbHelp.getOrgLevelContainer(),  // 
	    						dbHelp.getCompAccContainer()},
	    				new boolean[] { //false = insertable & updatable & deletable container
	    						        // true = read-only container
	    						false, //we can insert & delete & update orgpost from this view
	    						true, //orglevel is a read-only container for this view
	    						true  //compacc is a read-only container for this view
	    				},		
	    				new String[] {  //columns of joining (the pair column is always a primary key)
	    				  "orgschema.level",   // join orglevel on orgschema.level = orglevel.id
	    				  "orgschema.accid"  // join compacc on orgschema.accid = compacc.id
	    				},
	    				new int[] { // left outer join = 1, inner join = 0
	    	   					1,   //left outer join orglevel
	    	   				    1  // left outer join compacc	    	   				    
	    				},
	    				null,
	    				new String[] { //sorting: null or empty array assume no sorting
	    					"orglevel.level",
	    					"orgschema.pid",
	    					"orgschema.id"
	    				},
	    				null, //sorting order boolean[] ascending: null assumes always ascending 
                        null, //LinkedHashMap<String,String> ColumnNameAliases: null assumes no aliases	
                        null,
	    				null  //groupBy is not implemented yet (as of March 28, 2012)and must be null 
	    				);
	    	}
	    	return OrgschemaViewContainer;
	    }
	   
	   public AEViewContainer getOrgschemaViewContainerByPar(int Par){
		   	try {
				getOrgschemaViewContainer().removeAllContainerFilters();
				getOrgschemaViewContainer().addContainerFilter(new Equal("orgschema.pid",Par));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return OrgschemaViewContainer;
			   
	   }
	   public AEViewContainer getDictStatisticViewContainer() throws Exception
	    {
	    	if (DictStatisticViewContainer==null)
	    	{
	    		DictStatisticViewContainer = new AEViewContainer(
	    				"DictStatisticView",  //view name
	    				new AETableContainer[]{  // from DictStatistic,Period 
	    						dbHelp.getDictStatisticContainer(),
	    						dbHelp.getAPeriodPropsContainer()
	    						},
	    				new boolean[] { //false = insertable & updatable & deletable container
	    						        // true = read-only container
	    						false, //we can insert & delete & update orgpost from this view
	    						true  //compacc is a read-only container for this view
	    				},		
	    				new String[] {  //columns of joining (the pair column is always a primary key)
	    				  "dictstatistic.aperiod"   // join DictStatistic on orgschema.level = orglevel.id
	    				},
	    				new int[] { // left outer join = 1, inner join = 0
	    	   					0    
	    				},
	    				null,
	    				null,
	    				null, //sorting order boolean[] ascending: null assumes always ascending 
                        null, //LinkedHashMap<String,String> ColumnNameAliases: null assumes no aliases	
                        null,
	    				null  //groupBy is not implemented yet (as of March 28, 2012)and must be null 
	    				);
	    	}
	    	return DictStatisticViewContainer;
	    }
		
		public AEViewContainer getStatisticUseViewContainer() throws Exception
	    {
	    	if (StatisticUseViewContainer==null)
	    	{
	    		StatisticUseViewContainer = new AEViewContainer(
	    				"StatisticUseView",  //view name
	    				new AETableContainer[]{  // from DictStatistic,Period 
	    						dbHelp.getStatisticUsesContainer(),
	    						dbHelp.getDictStatisticContainer()
	    						},
	    				new boolean[] { //false = insertable & updatable & deletable container
	    						        // true = read-only container
	    						false, //we can insert & delete & update orgpost from this view
	    						true  //compacc is a read-only container for this view
	    				},		
	    				new String[] {  //columns of joining (the pair column is always a primary key)
	    				  "statisticuses.statid"   // join DictStatistic on orgschema.level = orglevel.id
	    				},
	    				new int[] { // left outer join = true, inner join = false
	    	   					0    
	    				},
	    				null,
	    				null,
	    				null, //sorting order boolean[] ascending: null assumes always ascending 
                       null, //LinkedHashMap<String,String> ColumnNameAliases: null assumes no aliases	
                       null,
	    				null  //groupBy is not implemented yet (as of March 28, 2012)and must be null 
	    				);
	    	}
	    	return StatisticUseViewContainer;
	    }
		
		 public AEViewContainer getStatisticContainer() throws Exception
		    {
		    	if (StatisticContainer==null)
		    	{
		    		StatisticContainer = new AEViewContainer(
		    				"StatisticView",  //view name
		    				new AETableContainer[]{  // from Orgschema,OrgLevel,compacc 
		    						dbHelp.getStatisticUsesContainer(),
		    						dbHelp.getOrgschemaContainer(),  // 
		    						dbHelp.getDictStatisticContainer(),
		    						dbHelp.getAPeriodPropsContainer(),
		    						dbHelp.getCompAccContainer()},
		    				new boolean[] { //false = insertable & updatable & deletable container
		    						        // true = read-only container
		    						true, //we can insert & delete & update orgpost from this view
		    						true, //
		    						true,
		    						true,
		    						true//
		    				},		
		    				new String[] {  //columns of joining (the pair column is always a primary key)
		    				  "statisticuses.postid",   // join orglevel on orgschema.level = orglevel.id
		    				  "statisticuses.statid",  // join compacc on orgschema.accid = compacc.id
		    				  "dictstatistic.aperiod",
		    				  "orgschema.accid"
		    				},
		    				new int[] {
		    	   					1,  
		    	   				    1,
		    	   				    1,
		    	   				    1
		    				},
		    				null,
		    				new String[] { //sorting: null or empty array assume no sorting
		    					"orgschema.name"
		    				},
		    				null, //sorting order boolean[] ascending: null assumes always ascending 
	                        null, //LinkedHashMap<String,String> ColumnNameAliases: null assumes no aliases	
	                        null,
		    				null  //groupBy is not implemented yet (as of March 28, 2012)and must be null 
		    				);
		    	}
		    	return StatisticContainer;
		    }
		
		public AEViewContainer getEventPostTaskContainer() throws Exception
	   {
		   if (EventPostTaskContainer==null)
		   {
			   EventPostTaskContainer = new AEViewContainer("EventPostTask",new AETableContainer[]{dbHelp.getOrgschemaReadContainer()},
		        		 new boolean[]{false},null,null,null,null,null,null,null,null);
		   }
		   return EventPostTaskContainer;
	   }

}
